package com.wf.classRoomTest;

import java.lang.annotation.*;

/**
 * @author mpk16
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface TestA{
    String[] value();
    int age() default 100;
    String name();
}

/**
 * @author mpk16
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface TestB{
    String name();
}

@TestB(name = "zzz")
@TestA(name = "yyy",value = {"xxx","xxx"})
public class AnnotationDemo02 {
    public static void main(String[] args) {
        // 获取类上的注解
        Annotation[] annotations = AnnotationDemo02.class.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof TestB) {
                TestB testB = (TestB) annotation;
                System.out.println("TestB name: " + testB.name());
            } else if (annotation instanceof TestA) {
                TestA testA = (TestA) annotation;
                System.out.println("TestA name: " + testA.name());
                System.out.println("TestA value: " + arrayToString(testA.value()));
                System.out.println("TestA age: " + testA.age());
            }
        }
    }

    // 辅助方法，将数组转换为字符串
    private static String arrayToString(String[] array) {
        StringBuilder sb = new StringBuilder();
        for (String element : array) {
            sb.append(element).append(", ");
        }
        return sb.toString().replaceAll(", $", ""); // 去掉最后一个逗号和空格
    }

    }

